/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.gae;

import org.vnetcon.blobdb.database.backend.KeyInterface;
import org.vnetcon.blobdb.database.backend.TableInterface;
import org.vnetcon.blobdb.database.backend.gae.jdo.Table;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;



public class GAETable implements TableInterface {

	Table nativeTable = null;
	
	public GAETable(Table nativeTable){
		this.nativeTable = nativeTable;
	}
	
	@Override
	public byte[] getBytes() {
		Blob b = this.nativeTable.getColumnInfo();
		return b.getBytes();
	}

	@Override
	public int getTableID() {
		return this.nativeTable.getTableID();
	}

	@Override
	public void setColumnInfo(byte[] infobytes) {
		Blob b = new Blob(infobytes);
		this.nativeTable.setColumnInfo(b);
	}

	@Override
	public TableInterface newInstance(int newTableId, String schema,
			String tableName, int tableRowSize, int tableSegMaxRows) {
		Table t = new Table(newTableId, schema, tableName, tableRowSize, tableSegMaxRows);
		GAETable gaet = new GAETable(t);
		return gaet;
	}

	@Override
	public KeyInterface getKey() {
		GAEKey retKey = new GAEKey();
		retKey.setKeyValue(this.nativeTable.getTableKey());
		return retKey;
	}

	@Override
	public void setTableKey(KeyInterface key) {
		Key nativeKey = (Key)key.getKeyValue();
		this.nativeTable.setTableKey(nativeKey);
	}

	@Override
	public String getNameForKey() {
		return Table.class.getSimpleName();
	}

	@Override
	public Object getNativeTable() {
		return this.nativeTable;
	}

	@Override
	public int getRowsize() {
		return this.nativeTable.getRowsize();
	}

	@Override
	public int getMaxrows() {
		return this.nativeTable.getMaxrows();
	}

}
